
package com.tenentia.pcmps.swm.business.implementation;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.tenentia.pcmps.swm.Constants;
import com.tenentia.pcmps.swm.business.BusinessLogicException;
import com.tenentia.pcmps.swm.business.interfaces.GroupsManager;
import com.tenentia.pcmps.swm.business.interfaces.UsersManager;
import com.tenentia.pcmps.swm.data.DataAccessObjectsFactory;
import com.tenentia.pcmps.swm.data.beans.ApplicationUser;
import com.tenentia.pcmps.swm.data.beans.UsersGroup;
import com.tenentia.pcmps.swm.data.interfaces.GroupsDAO;

public class GroupsManagerImpl implements GroupsManager {

	private static final Logger LOGGER = Logger.getLogger(GroupsManagerImpl.class.getPackage().getName());

	GroupsManagerImpl() {

		super();
	}

	@Override
	public void delete(UsersGroup group) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			dataAccessObject.delete(group);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}

	}

	@Override
	public UsersGroup get(UsersGroup group) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);

			UsersGroup methodResult = dataAccessObject.get(group);

			if (null != methodResult.getMembers() && methodResult.getMembers().size() > 0) {

				UsersManager usersManager = new UsersManagerImpl();

				Set<ApplicationUser> tmpSet = new HashSet<ApplicationUser>();

				for (Iterator<ApplicationUser> iterator = methodResult.getMembers().iterator(); iterator.hasNext();) {
					ApplicationUser tmpApplicationUser = iterator.next();
					tmpSet.add(usersManager.get(tmpApplicationUser));
				}

				methodResult.setMembers(tmpSet);
			}
			return methodResult;
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	@Override
	public List<UsersGroup> getAvailableGroups() throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			return dataAccessObject.getAvailableGroups();
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	@Override
	public List<UsersGroup> getTableItemsByRange(Object filterParameters, Object sortParameters, Integer firstRow, Integer numberOfRows) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			return dataAccessObject.getTableItemsByRange(filterParameters, sortParameters, firstRow, numberOfRows);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	@Override
	public Integer getTableRowCount(Object filterParameters) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			return dataAccessObject.getTableRowCount(filterParameters);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	public UsersGroup getUserGroup(ApplicationUser user) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			return dataAccessObject.getUserGroup(user);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	@Override
	public void save(UsersGroup group) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			dataAccessObject.save(group);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}

	}

	public UsersGroup setUserGroup(ApplicationUser user) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			return dataAccessObject.setUserGroup(user);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

	@Override
	public void update(UsersGroup group) throws BusinessLogicException {

		LOGGER.debug(Constants.STRING_L4J_METHOD_BEGIN);

		try {
			GroupsDAO dataAccessObject = (GroupsDAO) DataAccessObjectsFactory.getInstance().createNewDataAccessObject(DataAccessObjectsFactory.GROUPS_DAO);
			dataAccessObject.update(group);
		} catch (Exception e) {
			LOGGER.error(e, e);
			throw new BusinessLogicException(e);
		}
	}

}
